﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ceTerm
{
    public class Terminal
    {
        public static AT_UHF_NET.CUHFHost RFID;
        public static ATScan_NET.CScan Scanner;
        private static Util_PowerOn_NET.CUtil_PowerOn powerNotify;

        static Terminal()
        {
            if (Settings.IsInDebugMode) return;

            RFID = new AT_UHF_NET.CUHFHost();
            RFID.PowerON();

            if (!RFID.Open())
                throw new ApplicationException("RFID başlatılamadı!");


            RFID.SET_PowerControl((uint)Settings.Power.OkumaMax);   //0
            RFID.SET_ReportMode(AT_UHF_NET.ReportMode.EXTENDED_INFORMATION, true);
            RFID.SET_Session(0);
        }

        [DllImport("ATUTIL.dll", EntryPoint = "AT_SerialNo_Read", SetLastError = true)]
        internal static extern bool _AT_SerialNo_Read(string AT_SerialNo);

        private static int GetTerminalId()
        {
            string SN = new string(new char[16]);
            _AT_SerialNo_Read(SN);
            switch(SN.Substring(0, SN.IndexOf('\0')))
            {
                case "008703100691": return 1;
                case "008703101339": return 2;
                case "008701130021": return 3;
                default: return 3;
            }
        }

        public static void Start(Form form)
        {
            if (Settings.IsInDebugMode) return;

            powerNotify = new Util_PowerOn_NET.CUtil_PowerOn(form);
            powerNotify.AT_PowerNotifyHWND();
            powerNotify.AT_PowerNotifyEvent();
            Settings.TerminalId = GetTerminalId();

            Terminal.Scanner = new ATScan_NET.CScan(form);

            switch (Settings.TerminalId)
            {
                case 1:
                    if (!Terminal.Scanner.ScanOpen(ATScan_NET.CScan.ScannerType.Type1))
                        throw new ApplicationException("Barkod okuyucu başlatılamadı!");
                    break;

                case 2:
                    if (!Terminal.Scanner.ScanOpen(ATScan_NET.CScan.ScannerType.Type2))
                        throw new ApplicationException("Barkod okuyucu başlatılamadı!");
                    break;

                case 3:
                    if (!Terminal.Scanner.ScanOpen(ATScan_NET.CScan.ScannerType.Type2))
                        throw new ApplicationException("Barkod okuyucu başlatılamadı!");
                    break;
            }
        }

        public static void Stop()
        {
            Scanner.ScanClose();

            RFID.Close();
            RFID.PowerOFF();

            powerNotify.AT_PowerNotifyClose();
        }

        public static void StartBarcode()
        {
            Scanner.ScanStart();
        }

        public static void StopBarcode()
        {
            Scanner.ScanStop();
        }

        public static void StartRFID()
        {
            RFID.ReadEPC_Continuous();
            //RFID.ReadUID(AT_UHF_NET.UIDREAD_CODE.EPC_GEN2_MULTI_TAG);
            //RFID.SET_TimeOut(0);
            //RFID.SET_ContinueModeONOff(true);
        }

        public static void StopRFID()
        {
            //if (RFID.IsOpen())
                RFID.Stop();
        }

        public static void SetActivatedForm(Form form)
        {
            if (!Settings.IsInDebugMode)
            {
                RFID.ActivatedForm = form;
                Scanner.ActivatedForm = form;
            }
        }

        public static AT_UHF_NET.AccessResult WriteRFID2(int rfid)
        {
            return Terminal.RFID.BankSelectWrite_Done(AT_UHF_NET.MEMBANK_CODE.BANK_EPC, 2, rfid.ToString().PadLeft(24, '0'));
        }

        public static bool WriteRFID(int rfid)
        {
            return Terminal.RFID.BankSelectWrite(AT_UHF_NET.MEMBANK_CODE.BANK_EPC, 2, rfid.ToString().PadLeft(24, '0'));
        }

        public static void PlaySuccess()
        {
            AT_UHF_NET.CUHFHost.PlaySuccess();
        }

        public static void PlayFail()
        {
            AT_UHF_NET.CUHFHost.PlayFail();
        }

        //oku
        //Terminal.RFID.ReadUID(AT_UHF_NET.UIDREAD_CODE.EPC_GEN2_MULTI_TAG);


        //scanner.Vibrator(true);
        //Thread.Sleep(300);
        //scanner.Vibrator(false);

        //scanner.PlaySuccess();


        public static void Test()
        {
            /*
                         If the value of Code is set by EPC_GEN2_MULTI_TAG, the reader reads tag
            continuously till it receives the stop command.
                         * 
                         * 1.4.54. bool SET_TimeOut(uint ScanTime)
            bool SET_ScanTime(uint ScanTime)
            Description :
            This function sets up operation time. You can set up the scan-time by Second. And if
            you set up the scan-time by 0 sec, he reader reads tag continuously till it receives the
            stop command.
            Parameters :
            uint ScanTime – operation time(second)
            Return Values :
            true = success, false = failure
            Protocol Type : EPC Gen2
            */
        }

    }
}

